package lovemusic.lanou.com.newsmusic.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import lovemusic.lanou.com.newsmusic.R;
import lovemusic.lanou.com.newsmusic.models.MusicInfo;
import lovemusic.lanou.com.newsmusic.utils.CoverLoader;
import lovemusic.lanou.com.newsmusic.utils.UIUtils;

/**
 * 本地音乐列表的适配器
 * Created by lanouhn on 2016/9/29.
 */
public class LocalMusicAdapter extends BaseAdapter {
    private OnMoreClickListener mlistener;
    private List<MusicInfo> lists;
    private Context mContext;

    public LocalMusicAdapter(List<MusicInfo> lists, Context mContext) {
        this.lists = lists;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int i) {
        return lists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if (view==null){
            holder=new ViewHolder();
            view= UIUtils.inflate(R.layout.view_holder_music);
            holder.iv_cover= (ImageView) view.findViewById(R.id.iv_cover);
            holder.tv_title= (TextView) view.findViewById(R.id.tv_title);
            holder.tv_artist= (TextView) view.findViewById(R.id.tv_artist);
            holder.v_playing=view.findViewById(R.id.v_playing);
            holder.iv_more= (ImageView) view.findViewById(R.id.iv_more);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }
        if (i==mPlayingPosition){
            holder.v_playing.setVisibility(View.VISIBLE);
        }else {
            holder.v_playing.setVisibility(View.INVISIBLE);
        }
        Bitmap cover = CoverLoader.getInstance().loadThumbnail(lists.get(i).getCoverUri());
        holder.iv_cover.setImageBitmap(cover);
        holder.tv_artist.setText(lists.get(i).getArtist());
        holder.tv_title.setText(lists.get(i).getTitle());

        holder.iv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mlistener!=null){
                    mlistener.onMoreClick(i);
                }
            }
        });
        return view;
    }

    public static class ViewHolder{
        private View v_playing;
        private ImageView iv_cover,iv_more;
        private TextView tv_title,tv_artist;
    }

    public void setOnMoreClickListener(OnMoreClickListener mlistener){
        this.mlistener=mlistener;
    }
    public void setSelecterMusic(int playingPosition){
        this.mPlayingPosition=playingPosition;
    }
    private int mPlayingPosition=-1;
}
